export const chooseImage = (
    args = {
        count: 1,
        sourceType: ['album', 'camera'],
        sizeType: ['original', 'compressed']
    },
    uploadFn
) => {
    return new Promise((resolve, reject) => {
        uni.chooseImage({
            count: args.count || 1,
            sourceType: args.sourceType || ['album', 'camera'],
            sizeType: args.sizeType || ['original', 'compressed'],
            success: ({ tempFiles }) => {
                if (typeof uploadFn == Function || typeof uploadFn == 'function') {
                    uploadFn && resolve(uploadFn(tempFiles));
                } else {
                    resolve(tempFiles);
                }
            },
            fail: () => {
                reject([]);
            }
        });
    });
};

export const chooseLocation = (fn) => {
    return new Promise((resolve, reject) => {
        uni.chooseLocation({
            success: (res) => {
                const { latitude, longitude, name, address } = res;
                const fullAddress = name ? `${address}(${name})` : address;
                if (typeof fn == Function || typeof fn == 'function') {
                    fn && resolve(fn({ latitude, longitude, name, address, fullAddress }));
                } else {
                    resolve({ latitude, longitude, name, address, fullAddress });
                }
            },
            fail: (err) => {
                reject(err);
            }
        });
    });
};

export const payment = (orderInfo, provider = 'wxpay') => {
    return new Promise((resolve, reject) => {
        uni.requestPayment({
            provider,
            ...orderInfo,
            success: (res) => {
                resolve(res);
            },
            fail: (err) => {
                reject(err);
                console.debug('paymen fail:' + JSON.stringify(err));
            }
        });
    });
};
